package cn.melonlib.cores.jpa.model.bean.impl;

import cn.melonlib.cores.jpa.model.bean.OffsetQuery;
import cn.melonlib.cores.jpa.model.bean.PaginationListResult;
import cn.melonlib.cores.jpa.model.bean.PaginationQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
public class PaginationList<ENTITY> extends ArrayList<ENTITY> implements PaginationListResult<ENTITY> {

    private long total;

    private long page;

    private long limit;

    private long offset;

    public PaginationList(){

    }

    public PaginationList(List<ENTITY> data,long total,long start,long limit){
        super(data.size());
        this.addAll(data);
    }


    public long getRows() {
        return this.size();
    }

    @Override
    public List<ENTITY> getData() {
        return this;
    }


    public static <ENTITY> PaginationList createOffset(List<ENTITY> datas, OffsetQuery query, long total){
        PaginationList<ENTITY> paginationList= new PaginationList<>();
        paginationList.addAll(datas);
        paginationList.setOffset(query.getOffset());
        paginationList.setLimit(query.getLimit());
        paginationList.setTotal(total);
        return paginationList;
    }
    public static <ENTITY> PaginationList createOffset(List<ENTITY> datas, long offset, long limit, long total){
        PaginationList<ENTITY> paginationList= new PaginationList<>();
        paginationList.addAll(datas);
        paginationList.setOffset(offset);
        paginationList.setLimit(limit);
        paginationList.setTotal(total);
        return paginationList;
    }

    public static <ENTITY> PaginationList createPageInfo(List<ENTITY> datas, PaginationQuery query, long total){
        PaginationList<ENTITY> paginationList= new PaginationList<>();
        paginationList.setPage(query.getPage());
        paginationList.setLimit(query.getLimit());
        paginationList.setTotal(total);
        return paginationList;
    }
}
